package com.ukao.steward.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.ListBaseAdapter;
import com.ukao.steward.base.SuperViewHolder;
import com.ukao.steward.bean.HomeBean;
import com.ukao.steward.util.DensityUtils;

import java.util.List;

import static com.ukao.steward.R.id.image_view;

/**
 * Created by ukaodev on 2017/8/24.
 */

public class HomeAdapter extends ListBaseAdapter<HomeBean> {


    private boolean islogistics;

    public HomeAdapter(Context context, List<HomeBean> mHomeBean) {
        super(context, mHomeBean);
    }

    public  void  setPersonnelEnter(boolean  islogistics){
        this.islogistics=islogistics;

    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_home_layout;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        HomeBean item = mDataList.get(position);
        RelativeLayout   homeItemLayout=holder.getView(R.id.home_item_layout);
        ImageView imageView=holder.getView(image_view);
        TextView name=holder.getView(R.id.product_name);
        TextView count=holder.getView(R.id.count);
        TextView msg=holder.getView(R.id.msg);


        if (item.getName().equals(mContext.getString(R.string.cash_register_function))){
            holder.itemView.setBackgroundResource(R.color.transparent);
            ViewGroup.LayoutParams mParams = homeItemLayout.getLayoutParams();
            mParams.height= DensityUtils.dip2px(30);
            homeItemLayout.setLayoutParams(mParams);
            setSateVisibility(imageView, name, msg, View.GONE, View.VISIBLE);
        }else{
            setSateVisibility(imageView, name, msg, View.VISIBLE, View.GONE);
            holder.itemView.setBackgroundResource(R.drawable.shape_home_item_select);
            setLayoutParams(homeItemLayout);
            setMsgLayoutParams(imageView, count);


        }

        if (item.getDrawable()!=0){
            imageView.setImageResource(item.getDrawable());
        }

        if (position==7||position==8){
            msg.setText("");
        }else{
            name.setText(item.getName());
        }

        if (item.getMsgCount()==0){
            count.setVisibility(View.INVISIBLE);
        }else{
            count.setVisibility(View.VISIBLE);
            count.setText(item.getMsgCount()+"");

        }


    }

    void setSateVisibility(ImageView imageView, TextView name, TextView msg, int gone, int visible) {
        name.setVisibility(gone);
        imageView.setVisibility(gone);
        msg.setVisibility(visible);
    }

    /**
     * 设置消息显示大小
     * @param imageView
     * @param count
     */
    void setMsgLayoutParams(ImageView imageView, TextView count) {
        if (islogistics){
            ViewGroup.LayoutParams mImageParams=imageView.getLayoutParams();
            mImageParams.height= DensityUtils.dip2px(60);
            mImageParams.width= mImageParams.height;
            imageView.setLayoutParams(mImageParams);

            ViewGroup.LayoutParams mCountParams=count.getLayoutParams();
            mCountParams.height=DensityUtils.dip2px(30);
            mCountParams.width= mCountParams.height;
            count.setLayoutParams(mCountParams);
        }
    }







    /**
     * 根据人员设置布局高度
     * @param homeItemLayout
     */
    void setLayoutParams(RelativeLayout homeItemLayout) {
        ViewGroup.LayoutParams mParams = homeItemLayout.getLayoutParams();
        int dpValue = islogistics ?35 : 4;
        int horCount = islogistics ? 2: 3;
        mParams.height= (DensityUtils.getScreenW()-DensityUtils.dip2px(dpValue))/horCount;
        homeItemLayout.setLayoutParams(mParams);
    }

}
